package com.zzw.zim.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.zim.security.user.UserDetail;
import com.zzw.zim.system.dto.AddRoleMenuDTO;
import com.zzw.zim.system.entitys.SysRoleMenuEntity;
import com.zzw.zim.system.dao.SysRoleMenuDao;
import com.zzw.zim.system.service.SysRoleMenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenuEntity> implements SysRoleMenuService {

    @Override
    public String addRoleMenu(AddRoleMenuDTO dto) {
        UserDetail user = (UserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return baseMapper.insertBatch(dto.getMenuIds(),dto.getRoleId(),user.getUsername())>0?"新增成功":"新增失败";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editRoleMenu(AddRoleMenuDTO dto) {
        LambdaUpdateWrapper<SysRoleMenuEntity> wrapper = Wrappers.lambdaUpdate(SysRoleMenuEntity.class);
        wrapper.eq(SysRoleMenuEntity::getRoleId,dto.getRoleId());
        baseMapper.delete(wrapper);
        UserDetail user = (UserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return baseMapper.insertBatch(dto.getMenuIds(),dto.getRoleId(),user.getUsername())>0?"编辑成功":"编辑失败";
    }

    @Override
    public String removeByRoleId(String roleId) {
        return null;
    }
}
